﻿//-----------------------------------------------------------------------
// <copyright file="ColorSelector.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>29/07/2012</date>
//-----------------------------------------------------------------------

using ConsoleHoster.Common.Utilities;
using ConsoleHoster.View.Popups;
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace ConsoleHoster.View.Controls
{
	/// <summary>
	/// Interaction logic for ColorSelector.xaml
	/// </summary>
	public class ColorSelector : TextBoxWithActionButton
	{
		public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register("SelectedColor", typeof(Color), typeof(ColorSelector), new PropertyMetadata(Colors.Black, new PropertyChangedCallback(OnSelectedColor_Changed)));
		public static readonly DependencyProperty ColorCodeProperty = DependencyProperty.Register("ColorCode", typeof(string), typeof(ColorSelector), new PropertyMetadata("Black", new PropertyChangedCallback(OnColorCode_Changed)));

		private static void OnSelectedColor_Changed(DependencyObject argSender, DependencyPropertyChangedEventArgs argEA)
		{
			ColorSelector tmpSender = argSender as ColorSelector;
			tmpSender.ColorCode = ColorUtilities.GetColorNameOrCode(tmpSender.SelectedColor);
		}

		private static void OnColorCode_Changed(DependencyObject argSender, DependencyPropertyChangedEventArgs argEA)
		{
			ColorSelector tmpSender = argSender as ColorSelector;
			tmpSender.Button.Background = new SolidColorBrush(ColorUtilities.GetColorFromString(argEA.NewValue as String, Colors.Black));
			tmpSender.SelectedColor = ColorUtilities.GetColorFromString(argEA.NewValue as string, Colors.Black);
		}

		public ColorSelector()
		{
			InitializeComponent();

			Binding tmpBinding = new Binding
			{
				Path = new PropertyPath("ColorCode"),
				Source = this,
				Mode = BindingMode.TwoWay
			};
			this.SetBinding(TextBoxWithActionButton.TextProperty, tmpBinding);

			this.ButtonAction = () =>
			{
				ColorChooserView tmpColorChooser = new ColorChooserView();
				tmpColorChooser.Owner = this.GetRootWindow();
				tmpColorChooser.WindowStartupLocation = WindowStartupLocation.CenterOwner;
				tmpColorChooser.SelectedColor = this.SelectedColor;

				bool? tmpResult = tmpColorChooser.ShowDialog();
				if (tmpResult.HasValue && tmpResult.Value)
				{
					this.SelectedColor = tmpColorChooser.SelectedColor;
				}
			};
		}

		private Window GetRootWindow()
		{
			Window tmpResult = null;

			FrameworkElement tmpParent = this.Parent as FrameworkElement;
			while (tmpParent != null)
			{
				if (tmpParent is Window)
				{
					tmpResult = tmpParent as Window;
					break;
				}
				else
				{
					tmpParent = tmpParent.Parent as FrameworkElement;
				}
			}

			return tmpResult;
		}

		public string ColorCode
		{
			get
			{
				return this.GetValue(ColorCodeProperty) as String;
			}
			set
			{
				this.SetValue(ColorCodeProperty, value);
			}
		}

		public Color SelectedColor
		{
			get
			{
				return (Color)this.GetValue(SelectedColorProperty);
			}
			set
			{
				this.SetValue(SelectedColorProperty, value);
			}
		}
	}
}
